﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.PubDS
{
    /// <summary>
    /// 获取数据集详细信息
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("PubDS", "GetDataSource", "1.0")]
    public class GetDataSourceAction : BaseAction<GetDataSourceParams, GetDataSourceResult>
    {
        protected override ActionResult<GetDataSourceResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataSourceParams opParams)
        {
            SER.PubSourceService dtSerService = new SER.PubSourceService();
            ServiceResult<VIW.VPubDataSource> serResult = dtSerService.GetDetail(opParams.Id);
            if (serResult.IsSuccess)
            {
                GetDataSourceResult result = new GetDataSourceResult();

                result.Id = serResult.Data.Id;
                result.Type = serResult.Data.Type;
                result.Value = serResult.Data.Value;
                result.Description = serResult.Data.Description;
                result.Name = serResult.Data.Name;

                for (int index = 0; index < serResult.Data.Parameters.Count; index++)
                {
                    GetDataSourceResult.Parameter item = new GetDataSourceResult.Parameter();
                    item.Id = serResult.Data.Parameters[index].Id;
                    item.Name = DefaultValue.GetValue(serResult.Data.Parameters[index].Name, string.Empty);
                    item.Description = DefaultValue.GetValue(serResult.Data.Parameters[index].Description, string.Empty);
                    item.Type = DefaultValue.GetValue(serResult.Data.Parameters[index].Type, string.Empty);
                    item.Index = serResult.Data.Parameters[index].Index;

                    result.Parameters.Add(item);
                }
                return new ActionResult<GetDataSourceResult>(result);
            }
            else
            {
                return new ActionResult<GetDataSourceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataSourceParams
    {
        public Guid Id { get; set; }
    }

    public class GetDataSourceResult
    {
        public Guid Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public string Value { get; set; }

        public int Type { get; set; }

        public List<Parameter> Parameters = new List<Parameter>();

        public class Parameter
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }

            public string Type { get; set; }

            public int Index { get; set; }
        }
    }
}
